package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.LabResult;
import com.jims.master.modules.doctor.service.LabResultService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Created by Future on 2017/7/27.
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/lab/labResult")
public class LabResultController extends BaseController {

    @Autowired
    private LabResultService labResultService;

    @ModelAttribute
    public LabResult get(@RequestParam(required=false) String id) {
        LabResult entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = labResultService.get(id);
        }
        if (entity == null){
            entity = new LabResult();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(LabResult labResult, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<LabResult> page = labResultService.findPage(new Page<LabResult>(request, response), labResult);
        model.addAttribute("page", page);
        return "modules/doctor/lab/labResultIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(LabResult labResult, Model model) {
        model.addAttribute("labResult", labResult);
        return "modules/doctor/lab/labResultForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(LabResult labResult, Model model) {
        StringData data=new StringData();
        int i=labResultService.save(labResult);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(LabResult labResult) {
        StringData data=new StringData();
        int i=labResultService.delete(labResult);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }

}